package com.herui.iot.ui.view;

import android.graphics.Color;
import android.graphics.Typeface;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.herui.sport_care.config.ChartConfig;
import com.herui.sport_care.utils.DateUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by amanda.shan on 2022/12/14.
 */
public class PoweLineChartView {

    private LineChart mDoubleLineChar;

    private List<Entry> leftEntries = new LinkedList<>();

    private float leftMax = 20;

    public PoweLineChartView(LineChart mDoubleLineChar) {
        this.mDoubleLineChar = mDoubleLineChar;
    }

    public void refresh(){
        mDoubleLineChar.invalidate();
    }

    public void init() {

        initStyle();
        initData();

        //设置数值选择监听
//        mDoubleLineChar.setOnChartValueSelectedListener(this);
        // 没有描述的文本
        mDoubleLineChar.getDescription().setEnabled(false);
        // 支持触控手势
        mDoubleLineChar.setTouchEnabled(true);
        mDoubleLineChar.setDragDecelerationFrictionCoef(0.9f);
        // 支持缩放和拖动
        mDoubleLineChar.setDragEnabled(false);
        mDoubleLineChar.setScaleEnabled(false);
        mDoubleLineChar.setDrawGridBackground(false);
        mDoubleLineChar.setHighlightPerDragEnabled(true);
        // 如果禁用,扩展可以在x轴和y轴分别完成
        mDoubleLineChar.setPinchZoom(true);
        // 设置背景颜色(灰色)
        mDoubleLineChar.setBackgroundColor(Color.WHITE);
        //默认x动画
        mDoubleLineChar.animateX(0);
    }

    private void initStyle() {
        //x轴
        XAxis xAxis = mDoubleLineChar.getXAxis();
        xAxis.setTextSize(11f);
        xAxis.setTextColor(Color.BLACK);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawLabels(true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setEnabled(true);
        //自定义样式
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
                return DateUtils.sToMMss((int) value);
            }
        });
        xAxis.setAxisMaximum(20);

        //左边y轴
        YAxis leftAxis = mDoubleLineChar.getAxisLeft();
        leftAxis.setTextColor(0xffF1917D);
        leftAxis.setTextSize(ChartConfig.yLableSize);
        leftAxis.setAxisMaximum(20f);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setDrawGridLines(true);
        leftAxis.setGranularityEnabled(true);
        leftAxis.setDrawLabels(true);

        // 禁止右轴
        mDoubleLineChar.getAxisRight().setEnabled(false);

        setLegend();
    }

    public void setLegend(){
        //为了让大家更直观的看到效果，我这里设置了整个图形距离上下左右四个方向的边距
        //在后面会讲解
//        lc.setExtraOffsets(30,60,30,30);

        //得到Legend对象
        Legend legend = mDoubleLineChar.getLegend();
        legend.setDrawInside(false);//不绘制在图形内部
        legend.setTextSize(ChartConfig.legendLableSize);//设置文字大小
        legend.setTypeface(Typeface.DEFAULT_BOLD);//文字加粗
//        legend.setFormSize(50f);//设置图例的大小
//        legend.setTextColor(Color.BLUE);//设置文字颜色
//        legend.setFormToTextSpace(20f);//设置图例距离文字的距离
        legend.setForm(Legend.LegendForm.CIRCLE);//设置图例类型为线条
        legend.setYOffset(20f);//距离底部20dp
        //设置图例下、中对齐
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
    }

    private void initData() {
        LineDataSet set1;
        // 创建一个数据集,并给它一个类型
        set1 = new LineDataSet(leftEntries, "功率(W)");

        set1.setAxisDependency(YAxis.AxisDependency.LEFT);
        set1.isDrawValuesEnabled();
        set1.setColor(0xffF1917D);
        set1.setCircleColor(0xffF1917D);
        set1.setLineWidth(2f);
        set1.setCircleRadius(3f);
        set1.setFillAlpha(65);
        set1.setFillColor(0xffF1917D);
        set1.setDrawFilled(true);
        set1.setDrawCircleHole(false);

        // 创建一个数据集的数据对象
        LineData data = new LineData(set1);
        data.setValueTextColor(Color.WHITE);
        data.setValueTextSize(9f);

        //设置数据
        mDoubleLineChar.setData(data);
    }

    private Long startTime = null;
    public  synchronized  void addData(float v) {
        int second = 0;
        if (startTime == null ){
            startTime = System.currentTimeMillis();
        }else{
            second = (int) ((System.currentTimeMillis()-startTime)/1000);
        }
        leftEntries.add(new Entry(second, v));

        LineDataSet lineDataSet;

        lineDataSet = (LineDataSet) mDoubleLineChar.getData().getDataSetByIndex(0);
        lineDataSet.setValues(leftEntries);
        if (leftEntries.size() > 20) {
            XAxis xAxis = mDoubleLineChar.getXAxis();
            xAxis.setAxisMaximum(leftEntries.size());
        }

        if (leftMax <= v){
            leftMax = v+5;
            YAxis yAxis = mDoubleLineChar.getAxisLeft();
            yAxis.setAxisMaximum(leftMax);
        }

        mDoubleLineChar.getData().notifyDataChanged();
        mDoubleLineChar.notifyDataSetChanged();

        refresh();
    }

    public synchronized void clear(){
        leftEntries.clear();
        initData();
        startTime = null;

        XAxis xAxis = mDoubleLineChar.getXAxis();
        xAxis.setAxisMaximum(20);

        leftMax = 20;
        YAxis yAxis = mDoubleLineChar.getAxisLeft();
        yAxis.setAxisMaximum(leftMax);

        mDoubleLineChar.getData().notifyDataChanged();
        mDoubleLineChar.notifyDataSetChanged();

        refresh();
    }

}
